# -*- encoding: utf-8 -*-
from Common.mysql import db
from src.ApiTest.SqlData.Database.sql_database import SqlData
from Common.yaml_method import YamlMethod
from sqlalchemy.exc import SQLAlchemyError


class DeleteSQL:
    """
    删除SQL配置信息接口
    """

    @staticmethod
    def delete_sql(config_id):
        """
        删除SQL配置信息
        :param config_id: 配置ID
        :return:
        """

        code = YamlMethod().read_data('code.yaml')['code']

        info = SqlData.query.filter_by(id=config_id).first()

        if info is not None:
            try:
                db.session.delete(info)
                db.session.commit()
            except SQLAlchemyError as e:
                db.session.rollback()
                res = {
                    'code': code[5],
                    'data': [],
                    'message': '删除失败'
                }
                return res

            res = {
                'code': code[0],
                'data': [],
                'message': '删除成功'
            }
            return res

        else:
            res = {
                'code': code[3],
                'data': [],
                'message': 'SQL配置信息不存在'
            }
            return res


